package cn.kinoko.service.manga.impl;

import cn.kinoko.common.aspect.annotation.Cacheable;
import cn.kinoko.common.base.model.BaseServiceImpl;
import cn.kinoko.common.constant.CacheKeyConstant;
import cn.kinoko.common.utils.CollectionUtil;
import cn.kinoko.mapper.manga.MangaCnMemberMapper;
import cn.kinoko.model.cnTeam.vo.PositionTreeVO;
import cn.kinoko.model.cnTeam.vo.MemberVO;
import cn.kinoko.model.manga.entity.MangaCnMember;
import cn.kinoko.service.common.CommonService;
import cn.kinoko.service.manga.MangaCnMemberService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author kinoko
 */
@AllArgsConstructor
@Service
public class MangaCnMemberServiceImpl extends BaseServiceImpl<MangaCnMemberMapper, MangaCnMember> implements MangaCnMemberService {

    private CommonService commonService;

    /**
     * 漫画成员列表
     *
     * @param mangaId 漫画id
     * @return 成员列表
     */
    @Override
    public List<MemberVO> queryMangaMember(Long mangaId) {
        // 查询成员列表
        List<MemberVO> memberList = getBaseMapper().queryMangaMember(mangaId);
        return commonService.handleMemberPosition(memberList);
    }

    /**
     * 查询用户加入的漫画坑组
     *
     * @param uid 用户id
     * @return 漫画id列表
     */
    @Cacheable(key = CacheKeyConstant.USER_JOIN_MANGA_KEY, params = "#uid")
    @Override
    public List<Long> queryJoinMangaByUid(Long uid) {
        return lambdaQuery().select(MangaCnMember::getMangaId).eq(MangaCnMember::getUid, uid).list().stream()
                .map(MangaCnMember::getMangaId).toList();
    }

    /**
     * 查询职位树
     *
     * @param mangaId 漫画id
     * @param uidList 用户id列表
     * @return 职位树
     */
    @Override
    public Map<Long, PositionTreeVO> queryMangaPositionTree(Long mangaId, Collection<Long> uidList) {
        // 查询成员列表
        List<MemberVO> memberList = getBaseMapper().queryMangaMember(mangaId);
        if (CollectionUtil.isNotEmpty(uidList)) {
            memberList = memberList.stream().filter(memberVO -> uidList.contains(memberVO.getUid())).toList();
        }
        return commonService.handleMangaPositionTree(memberList);
    }
}